package com.briup.mt.service.impl;

import com.briup.mt.bean.Category;
import com.briup.mt.bean.CategoryExample;
import com.briup.mt.dao.ArticleMapper;
import com.briup.mt.dao.CategoryMapper;
import com.briup.mt.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service
public class CategoryImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> query(String name, String description) {
        CategoryExample categoryExample=new CategoryExample();

        if(!StringUtils.isEmpty(name)){
            categoryExample.createCriteria().andNameLike("%"+name+"%");
        }
        if(!StringUtils.isEmpty(description)){
            categoryExample.createCriteria().andDescriptionEqualTo(description);
        }

        List<Category> list = categoryMapper.selectByExample(categoryExample);
        return list;
    }

    @Override
    public void saveOrUpdate(Category category) {
        if(category.getId() == null){
            categoryMapper.insert(category);
        } else {
            categoryMapper.updateByPrimaryKey(category);
        }
    }

    @Override
    public void deleteById(long id) {
        categoryMapper.deleteByPrimaryKey(id);

    }
}
